package repository

import (
	"go-web/config"
	"go-web/model"
	"time"
)

type BorrowInfo struct {
	MemberName string
	BookTitle  string
	BorrowDate time.Time
}

func FindBookBorrowedByMemberId(memberId int) []model.Borrow {

	var records []model.Borrow
	config.DB.Preload("Book").Preload("Member").Where("member_id = ?", memberId).Find(&records)
	return records
}

func FindBorrowInfoByMemberId(memberId int) []BorrowInfo {

	var infos []BorrowInfo
	config.DB.Table("borrows").
		Select("members.name as member_name, books.title as book_title, borrows.borrow_date").
		Joins("left join members on members.id = borrows.book_id").
		Joins("left join books on books.id = borrows.book_id").
		Where("member_id = ?", memberId).Scan(&infos)
	return infos
}
